package com.bipbip.repositorio;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bipbip.modelo.Empresa;

@Repository(value = "empresaDao")
@Transactional(readOnly = true)
public class JPAEmpresaDAOImpl implements EmpresaDAO {

	private EntityManager em = null;

	/*
	 * Sets the entity manager.
	 */
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public Empresa select(int id) {
		return em.find(Empresa.class, id);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Empresa insert(Empresa e) {
		em.persist(e);
		return e;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(Empresa e) {
		em.remove(em.merge(e));
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public Empresa update(Empresa e) {
		return em.merge(e);
	}

	public List<Empresa> selectAll() {
		return em.createQuery("select e from Empresa e").getResultList();
	}

	public List<Empresa> selectBy(String attr, String value) {
		return em.createQuery("select e from Empresa e where "+ attr +"=" +"'"+value+"'").getResultList();
	}

}
